package cn.yedong.product.service.impl;

import cn.yedong.basic.service.IBaiduAiAuditService;
import cn.yedong.basic.util.AjaxResult;
import cn.yedong.basic.util.BaiduAiUtils;
import cn.yedong.product.domain.Product;
import cn.yedong.product.domain.ProductDetail;
import cn.yedong.product.domain.ProductOnlineAuditLog;
import cn.yedong.product.mapper.ProductDetailMapper;
import cn.yedong.product.mapper.ProductMapper;
import cn.yedong.product.service.IProductOnlineAuditLogService;
import cn.yedong.product.service.IProductService;
import cn.yedong.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private IProductOnlineAuditLogService productOnlineAuditLogService;

    /**
     * 添加服务的时候，添加服务详情
     * @param product
     */
    @Override
    public void add(Product product) {
        super.add(product);//添加之后产生自增id
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail != null){
            productDetail.setProductId(product.getId());//设置id产生关联
            productDetailMapper.add(productDetail);
        }

    }

    /**
     * 删除服务的时候，删除服务详情
     * @param id
     */
    @Override
    public void del(Long id) {
            productDetailMapper.delProductDetailByProductId(id);
        super.del(id);
    }

    /**
     * 修改服务的时候，修改服务详情
     * @param product
     */
    @Override
    public void update(Product product) {
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail != null){
            //改写sql，修改productDetail时不修改productId
            productDetailMapper.update(productDetail);
        }
        super.update(product);
    }
    /**
     * 批量删除服务的时候，批量删除服务详情
     */
    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    /**
     * 服务上架
     * @param ids
     * @return
     */
    @Override
    public AjaxResult onSale(List<Long> ids) {
        for (Long id : ids) {
            //查询每一个服务对象
            Product product = productMapper.loadById(id);
            //百度AI智能审核
            Boolean textAudit = baiduAiAuditService.textAudit(product.getName());
            Boolean imageAudit = baiduAiAuditService.imageAudit(product.getResources());
            if(textAudit && imageAudit){
                //上架：设置state=1；设置上架时间
                productMapper.updateOnSaleState(id,new Date());
                //添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setNote(id+"审核通过");
                log.setState(1);
                productOnlineAuditLogService.add(log);
            }else {
                //添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setNote(id+"审核失败");
                log.setState(0);
                productOnlineAuditLogService.add(log);
                return new AjaxResult(false,"信息审核失败，请重新提交！");
            }
        }
        return new AjaxResult();
    }

    /**
     * 服务下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        productMapper.offSale(ids,new Date());
    }
}
